package com.iqiyi.pps.epg.web.webapp.action.basement;


import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.model.basement.ConstantsMap;
import com.iqiyi.pps.epg.core.service.basement.ConstantsMapService;
import com.iqiyi.pps.epg.core.utils.StringUtil;
import com.iqiyi.pps.epg.web.utils.Struts2Utils;
import com.iqiyi.pps.epg.web.webapp.action.BaseAction;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: fangying
 * Date: 12-8-27
 * Time: 下午11:49
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/basement/constantsmap")
@Scope("prototype")
@Results({@Result(name = BaseAction.RELOAD, location = "constantsmap.action", type = "redirect")})
public class ConstantsMapAction extends BaseAction<ConstantsMap>{

    @Autowired
    private ConstantsMapService constantsMapService;

    private Long id;
    private ConstantsMap entity;
    private Page<ConstantsMap> page = new Page<ConstantsMap>(20);//每页5条记录

    @Override
    @Action(value="list")
    public String list() throws Exception {
        List<PropertyFilter> filters = PropertyFilter.buildFromHttpRequest(Struts2Utils.getRequest());
        //设置默认排序方式
        if (!page.isOrderBySetted()) {
            page.setOrderBy("id");
            page.setOrder(Page.ASC);
        }
        page = constantsMapService.getConstantsMapList(page,filters);
        return SUCCESS;
    }

    @Override
    @Action(value="input")
    public String input() throws Exception {
        return INPUT;
    }

    @Override
    @Action(value="save")
    public String save() throws Exception {
        String oldName=Struts2Utils.getParameter("oldName");
        String oldCode=Struts2Utils.getParameter("oldCode");
        if(entity!=null&&id!=null){    //保存
            if ((oldName!=entity.getName())&&!constantsMapService.isPropertityUnique("name",oldName, entity.getName(),entity.getType())) {
                addActionMessage("保存出错，相同类型下的常量名称重复！");
                return INPUT;
            }
            if ((oldCode!=entity.getCode())&&!constantsMapService.isPropertityUnique("code",oldCode, entity.getCode(),entity.getType())) {
                addActionMessage("保存出错，相同类型下的常量编码重复！");
                return INPUT;
            }
            constantsMapService.save(entity);
            addActionMessage("保存成功！");
        }else if(entity!=null&&id==null){ //新增
            if (!constantsMapService.isPropertityUnique("name",null, entity.getName(),entity.getType())) {
                addActionMessage("保存出错，相同类型下的常量名称重复！");
                return INPUT;
            }
            if (!constantsMapService.isPropertityUnique("code",null, entity.getCode(),entity.getType())) {
                addActionMessage("保存出错，相同类型下的常量编码重复！");
                return INPUT;
            }
            constantsMapService.save(entity);
            addActionMessage("保存成功！");
        }
        return INPUT;
    }

    @Override
    @Action(value="delete")
    public String delete() throws Exception {
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if(id!=null){
            entity=constantsMapService.get(id);
            if(entity==null){
                entity=new ConstantsMap();
            }
        }else{
            entity=new ConstantsMap();
        }

    }

    @Action("check_unique")
    public String check_unique()throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        String checkType=request.getParameter("checkType");
        if(checkType!=null&&!checkType.equals("")){
            String old=request.getParameter("old");
            String newValue=request.getParameter(checkType);
            Integer type= StringUtil.toInteger(request.getParameter("type"));
            if (constantsMapService.isPropertityUnique(checkType,old, newValue,type)) {
                Struts2Utils.renderText("true");
                return null;
            }
        }
        Struts2Utils.renderText("false");
        return null;
    }
    @Override
    public ConstantsMap getModel() {
        return entity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public ConstantsMap getEntity() {
        return entity;
    }

    public void setEntity(ConstantsMap entity) {
        this.entity = entity;
    }

    public Page<ConstantsMap> getPage() {
        return page;
    }

    public void setPage(Page<ConstantsMap> page) {
        this.page = page;
    }
}
